package com.ruoyi.partybuilding.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.partybuilding.common.EQuery.IDeptEntity;
import com.ruoyi.partybuilding.domain.NewsType;
import com.ruoyi.partybuilding.domain.vo.DeptOrgVo;
import com.ruoyi.partybuilding.domain.vo.NewsVo;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;

import lombok.var;
import com.ruoyi.common.core.domain.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.partybuilding.domain.News;
//import com.ruoyi.partybuilding.service.NewsService;
import com.ruoyi.partybuilding.common.*;

/**news
 * 资讯新闻;(pb_news)表控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2024-12-5
 */
@Api(tags = "资讯新闻对象功能接口")
@RestController
@RequestMapping("/partybuilding/News")
public class NewsController extends EQueryController<News>{

    @Anonymous
    @ApiOperation("详情")
//    @PreAuthorize("@ss.hasPermi('partybuilding:News:query')")
    @GetMapping(value = "/{id}")
    public R<News> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }

    @DataScope()
    @ApiOperation("分页")
    @PreAuthorize("@ss.hasPermi('partybuilding:News:list')")
    @GetMapping("query")
    public R<Page<News>> query(News whereObj, PageDomain pageRequest) {
        return super.queryPage(whereObj, pageRequest);
    }

    @Anonymous
    //@DataScope()
    @ApiOperation("列表,对query的rouyi兼容包装")
//    @PreAuthorize("@ss.hasPermi('partybuilding:News:list')")
    @GetMapping("list")
    public TableDataInfoRows<NewsVo> list(News entity, Long[] typeIds, PageSort pageRequest) {
        var ids = typeIds;
        var page = queryHelper.queryable(News.class)
                .whereObject(entity)
                .where(typeIds != null, p -> p.in(News::getTypeId, ids))
                .orderByObject(pageRequest != null, pageRequest)
                .orderByDesc(p->p.column(News::getId))
                .toPageResult(pageRequest.getPageNum(), pageRequest.getPageSize());
        var select = queryHelper.queryable(NewsType.class)
                .where(p -> p.in(NewsType::getId, page.getData().stream().map(News::getTypeId).toArray())).toList();
        var re = DataUtil.findSetRe(page.getData(), select,
                (p, p1) -> Objects.equals(p.getTypeId(), p1.getId()),
                (p, p1) -> {
                    NewsVo vo = DataUtil.copyProperties(p, new NewsVo());
                    if (p1 != null)
                        vo.setTypeName(p1.getName());
                    return vo;
                });
        return new TableDataInfoRows(page.getTotal(), re);
    }


    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('partybuilding:News:add')")
    @Log(title = "资讯新闻", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public R<Long> add(@RequestBody News entity){
        entity.setCreatorId(getUserId());
        return super.add(entity);
    }
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('partybuilding:News:remove')")
    @Log(title = "资讯新闻", businessType = BusinessType.DELETE)
    @PostMapping("remove/{ids}")
    public R<Long> remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }
    @ApiOperation("编辑")
    @PreAuthorize("@ss.hasPermi('partybuilding:News:edit')")
    @Log(title = "资讯新闻", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public  R<Long> edit(@RequestBody News entity){
        return super.edit(entity);
    }
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('partybuilding:News:export')")
    @Log(title = "资讯新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(News entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }
}